<?php
/**
 * Created by PhpStorm.
 * User: FDG348
 * Date: 2017/12/16
 * Time: 14:11
 */
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\GoodsCategory;
use backend\models\GoodsTag;
use backend\models\ContentArticleCategory;
use backend\models\ContentArticle;
use backend\models\GoodsBrand;
use backend\models\HotSaleConfig;

class SeoController extends AdminController{
    public $service = 'Seo';
    /**
     * 查询seo
     */
    public function actionFindSeo(){
        $data = Yii::$app->request->post();
        try {
            if (!$data['id']){
                throw new \Exception('需要修改的seo信息不存在');
            }
            if (!$data['type']){
                throw new \Exception('修改的seo类型不存在');
            }
            $id = intval($data['id']);
            $type = intval($data['type']);
            $table = chooseTable($type);
            $find_data = $table->find()->select('seo_title,seo_keywords,seo_description')->where('id=:id',[':id'=>$id])->asArray()->one();
            $this->retJson($find_data, 200);
        } catch (\Exception $e) {
            //add by allen.qiang 20180112 异常处理，日志优化 start
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            //add by allen.qiang 20180112 异常处理，日志优化 end
            $this->retJson($e->getMessage(), 500);
        }
    }
    /**
     * 修改seo
     */
    public function actionSaveSeo(){
        $data = Yii::$app->request->post();
        if (!$data['id']){
            throw new \Exception('需要修改的seo信息不存在');
        }
        if (!$data['type']){
            throw new \Exception('修改的seo类型不存在');
        }
        $id = intval($data['id']);
        $type = intval($data['type']);
        $table = chooseTable($type);
        $table_update = $table->findOne($id);

        $table_update->seo_title = !empty($data['seo_title'])?$data['seo_title']:null;
        $table_update->seo_keywords = !empty($data['seo_keywords'])?$data['seo_keywords']:null;
        $table_update->seo_description = !empty($data['seo_description'])?$data['seo_description']:null;

		$table_update->save();
        $update_id = $table_update->attributes['id'];
        if (!$update_id){
            $this->retJson('更新失败', 500);
        }
        $this->retJson('成功', 200);
    }
}

function chooseTable($type){
    switch ($type){
        case SEO_CATEGORY:
            $table = new GoodsCategory();
            break;
        case SEO_TAG:
            $table = new GoodsTag();
            break;
        case SEO_ARTICLE_CATEGORY:
            $table = new ContentArticleCategory();
            break;
        case SEO_ARTICLE:
            $table = new ContentArticle();
            break;
        case SEO_BRAND:
            $table = new GoodsBrand();
            break;
        case SEO_BRAND_STREET:
            $table = new HotSaleConfig();
            break;
        default:
            $table = new GoodsCategory();
            break;
    }
    return $table;
}